package com.boboPlanet.dict.service.impl;

import com.boboPlanet.comm.entity.dto.UploadPPTDTO;
import com.boboPlanet.comm.entity.po.FileDetailPO;
import com.boboPlanet.comm.exception.BusinessException;
import com.boboPlanet.comm.filestorage.PlanetFileBaseTemplate;
import com.boboPlanet.dict.mapper.PlanetFileMapper;
import com.boboPlanet.dict.service.PlanetFileService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.Contended;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2023/7/11
 * @Company planet
 **/
@Service
@Slf4j
public class PlanetFileServiceImpl implements PlanetFileService {

    @Resource
    private PlanetFileMapper planetFileMapper;

    //@Resource
    //private PPTOperateTemplate pptOperateTemplate;

    @Resource
    private PlanetFileBaseTemplate planetFileBaseTemplate;

    @Override
    public Boolean saveFile(FileDetailPO fileDetail) {
        if (fileDetail == null){
            return false;
        }

        //存在相同地址的覆盖
        FileDetailPO file = this.getFileByUrl(fileDetail.getUrl());
        if (file != null){
            Boolean del = this.deleteFileById(file.getId());
            if (!del){
                return false;
            }
        }
        return planetFileMapper.saveFile(fileDetail);
    }

    @Override
    public FileDetailPO getFileByUrl(String url) {
        if (StringUtils.isBlank(url)){
            throw BusinessException.build("获取文件信息失败,文件路径不能为空");
        }
        return planetFileMapper.getFileByUrl(url);
    }

    @Override
    public FileDetailPO getFileById(Long id) {
        if (id == null || id <= 0){
            return null;
        }
        return planetFileMapper.getFileById(id);
    }

    @Override
    public Boolean deleteFileById(Long id) {
        if (id == null || id <= 0){
            return false;
        }
        return planetFileMapper.deleteFileById(id);
    }

    @Override
    @SneakyThrows
    public Boolean updatePPTContent(UploadPPTDTO pptDto) {

        // pptOperateTemplate.replacePPTContent(pptDto);
        return true;
    }

    @Override
    @SneakyThrows
    public Boolean updatePPTContentByFile(MultipartFile file, UploadPPTDTO pptDto) {

       // pptOperateTemplate.replacePPTContentWithStream(pptDto,file.getInputStream());

        return true;
    }
}
